package cn.com.mayue.guava.basic;

import cn.com.mayue.guava.Person;
import cn.com.mayue.guava.PersonComparator;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.*;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by mayue on 2016/12/4.
 * 链式排序器 使用方便
 */
public class OrderingTest {
    @Test
    public void testOrdering() {
        List<String> list = Lists.newArrayList();
//        List<String> l = new ArrayList<String>();
        list.add("peida");
        list.add("jerry");
        list.add("harry");
        list.add("eva");
        list.add("jhon");
        list.add("neron");

        System.out.println("list:" + list);

//        使用Comparable类型的自然顺序， 例如：整数从小到大，字符串是按字典顺序;
        System.out.println("natural:" + Ordering.natural().sortedCopy(list));
        System.out.println("reverse:" + Ordering.natural().reverse().sortedCopy(list));
//        使用toString()返回的字符串按字典顺序进行排序；
        System.out.println("usingToString:" + Ordering.usingToString().sortedCopy(list));
    }

    /**
     *
     */
    @Test
    public void testPersonReverse(){
        Person personOne = new Person("testOne", 10);
        Person personTwo = new Person("testTwo", 20);
        Person personThree = new Person("testThree", 30);
        Person personFour = null;
        List<Person> personList = Lists.newArrayList(personOne, personTwo, personThree,personFour);
        //person 按名称倒序  存在null的则放在前面
        Ordering ordering = Ordering.from(new PersonComparator()).reverse().nullsFirst();
        System.out.println(ordering.sortedCopy(personList));


        personList.remove(personFour);
        Ordering ascOrdering = Ordering.from(new PersonComparator());
        //获取最大的2个
        System.out.println(ascOrdering.greatestOf(personList,2));
        //获取最小的一个
        System.out.println(ascOrdering.leastOf(personList,1));
    }

}
